<template>
  <view class="login-container">
    <uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
    <button type="primary" class="btn-login" @click="userLogin">一键登录</button>
    <view class="tips-text">
      登录后尽享更多权益
    </view>
  </view>
</template>

<script>
  import {
    mapMutations,
    mapState
  } from 'vuex'
  export default {
    name: "my-login",
    data() {
      return {

      };
    },
    methods: {
      ...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateRedirectInfo']),
      userLogin() {
        // #ifndef H5
        uni.getUserProfile({
          desc: "获取用户头像，昵称信息",
          success: (res) => {
            this.updateUserInfo(res.userInfo)
            this.getToken(res)
          },
          fail: () => {
            uni.$showMsg('登录失败')
          }
        })
        // #endif
        // #ifdef H5
        let userinfo = {
          nickName: "roozen.xyz",
          gender: 0,
          language: "zh_CN",
          city: "",
          province: "",
          country: "",
          avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLg2vgKPQCKetUf6htgRhXNIv1BLzPNLmgZLvJy21iaPyr47SeDib9JRUM3sib6ib5pKZD8neMPkm4ZUQ/132"
        }
        this.updateUserInfo(userinfo)
        this.getToken()
        // #endif
      },
      async getToken(info) {
        // #ifndef H5
        const [err, res] = await uni.login().catch(err => err)

        // 判断是否 uni.login() 调用失败
        if (err || res.errMsg !== 'login:ok') return uni.$showError('登录失败！')
        // 准备参数对象
        const query = {
          // code: res.code,
          code: res.code,
          encryptedData: info.encryptedData,
          iv: info.iv,
          rawData: info.rawData,
          signature: info.signature
        }
        // 获取 token
        const {
          data: loginResult
        } = await uni.$http.post('/api/public/v1/users/wxlogin', query)
        // #endif
        // if (loginResult.meta.status !== 200) return uni.$showMsg('登录失败！')
        uni.$showMsg('登录成功')
        // console.log(loginResult)
        // this.updateToken(loginResult.message.token)
        this.updateToken(
          'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
        )
        if (this.redirectInfo) {
          // 判断 vuex 中的 redirectInfo 是否为 null
          // 如果不为 null，则登录成功之后，需要重新导航到对应的页面
          this.navigateBack()
        }
      },
      navigateBack() {
        if (this.redirectInfo && this.redirectInfo.openType === 'switchTab') {
          // 调用小程序提供的 uni.switchTab() API 进行页面的导航
          uni.switchTab({
            // 要导航到的页面地址
            url: this.redirectInfo.from,
            // 导航成功之后，把 vuex 中的 redirectInfo 对象重置为 null
            complete: () => {
              this.updateRedirectInfo(null)
            }
          })
        }
      }
    },
    computed: {
      ...mapState('m_user', ['redirectInfo'])
    }
  }
</script>

<style lang="scss" scoped>
  .login-container {
    height: 80%;
    background-color: #F8F8F8;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;


    &::after {
      content: '';
      display: block;
      width: 100%;
      height: 40px;
      background-color: white;
      position: absolute;
      bottom: 0;
      left: 0;
      border-radius: 100%;
      transform: translateY(50%);
    }

    .btn-login {
      width: 90%;
      border-radius: 100px;

      // #ifdef APP-PLUS || MP
      margin: 30rpx 0;
      // #endif

      //#ifdef H5
      margin: 80rpx 0 15px 0;
      //#endif
      background-color: #C00000;

      .tips-text {
        font-size: 24rpx;
        color: gray;
      }
    }
  }
</style>
